package com.example.user.customviewdemo.pulltorefresh;

import android.animation.ValueAnimator;
import android.content.Context;
import android.database.DataSetObserver;
import android.graphics.Color;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;

/**
 * Created by user on 2016/6/6.
 * author:wangsfine
 * discre：完全自主开发
 */
public class PullToRefreshListView extends LinearLayout {
    private ListView listView;
    private View hideView;
    private LinearLayout.LayoutParams hideViewParams;
    private OnRefreshListener onRefreshListener;

    public interface OnRefreshListener{
        void onRefresh();
    }

    public void setOnRefreshListener(OnRefreshListener onRefreshListener){
        this.onRefreshListener = onRefreshListener;
    }
    public PullToRefreshListView(Context context) {
        super(context);
        init(context);
    }

    public PullToRefreshListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public PullToRefreshListView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init(context);
    }

    private void init(Context context){
        this.setOrientation(LinearLayout.VERTICAL);
        // 添加listview
        LinearLayout.LayoutParams listViewParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
        listView = new ListView(context);
        listView.setLayoutParams(listViewParams);


        //添加下拉时显示的隐藏view
        hideViewParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,0);
        hideView = new View(context);
        hideView.setVisibility(View.GONE);
        hideView.setBackgroundColor(Color.GRAY);
        hideView.setLayoutParams(hideViewParams);

        this.addView(hideView);
        this.addView(listView);
    }

    public void setAdapter(ListAdapter adapter){
        listView.setAdapter(adapter);
        adapter.registerDataSetObserver(new DataSetObserver() {
            @Override
            public void onChanged() {
                super.onChanged();
                hideView.setVisibility(View.GONE);
            }
        });
    }

    private float preY;
    @Override
    public boolean dispatchTouchEvent(MotionEvent ev) {
        switch (ev.getAction()){
            case MotionEvent.ACTION_DOWN:
                preY = ev.getY();
                break;
            case MotionEvent.ACTION_MOVE:
                float dy = ev.getY() - preY;
                if(dy > 0 && listView.getFirstVisiblePosition() == 0){
                    hideView.setVisibility(VISIBLE);
                    hideViewParams.height = (int)dy;
                    hideView.setLayoutParams(hideViewParams);
                    return true;
                }
                break;

            case MotionEvent.ACTION_UP:
                if(ev.getY() - preY < 100){
                    startAnimation(0);
                    break;
                }
                onRefreshListener.onRefresh();
               startAnimation(100);
                break;
        }
        return super.dispatchTouchEvent(ev);
    }

    private void startAnimation(float y){
        ValueAnimator animator = ValueAnimator.ofFloat(hideViewParams.height,y);
        animator.setInterpolator(new LinearInterpolator());
        animator.setDuration(80);
        animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float height = (float) animation.getAnimatedValue();
                hideViewParams.height = (int) height;
                hideView.setLayoutParams(hideViewParams);
            }
        });
        animator.start();
    }


}
